#include "threadPool.h"
#include "worker.h"
#include "path.h"
#include "puts_gets.h"
#include "quitCurrentUser.h"
#include "removeFile.h"
#include "makeDir.h"
#include "deleteCurrentUser.h" 
#include "password.h"
int parsingCommand(int netfd,pathStack_t* ppathStack,char* path,MYSQL * mysql){
   printf("Entering parsingCommand\n");
    cmd_t cmd;
    train_t train;
    int flag;
    int ret=recv(netfd,&train,sizeof(train_t),0);
    printf("ret=%d\n",ret);
    if(ret ==0){
        return -1;//返回-1表示退出
    }
    printf("%d\n",train.flag);

    memcpy(&cmd,train.data,sizeof(cmd));
    printf("type=%d\n",cmd.cmdType);
    if(train.flag == TRAIN_COMMAND){
        printf("cmd_type=%d",cmd.cmdType);
        if(cmd.cmdType == CMD_CD){
            printf("im cd %d\n",CMD_CD);
            changeDir(ppathStack,netfd,&cmd,mysql);
        }

        if(cmd.cmdType == CMD_LS){
            printf("I am ls = %d\n",CMD_LS);
            listFile(ppathStack,netfd,&cmd,mysql);
        }

       if(cmd.cmdType == CMD_PWD){
            printf("I am pwd = %d\n",CMD_PWD);
            printWokringDir(ppathStack,netfd,&cmd,mysql);
        }

      
        if(cmd.cmdType == CMD_PUTS){
        
            printf("I am puts = %d\n",CMD_PUTS);
            int ret=  server_puts(netfd,ppathStack,&cmd,mysql);
            if(ret==-1){
                return -1;
            }
        }

        if(cmd.cmdType == CMD_GETS){
            printf("I am gets = %d\n",CMD_GETS);
           int ret = server_gets(netfd,ppathStack,&cmd,mysql);
           if(ret==-1){
               return -1;
           }
        }

        if(cmd.cmdType == CMD_RM){
            printf("I am removeFile = %d\n",CMD_RM);
            removeFile(ppathStack,netfd,&cmd,mysql);
        }
        
        if(cmd.cmdType == CMD_MKDIR){
            printf("I am mkdir %d\n",CMD_MKDIR);
            makeDir(ppathStack,netfd,&cmd,mysql);
        }
        
       
        if(cmd.cmdType == CMD_RMDIR){
       
            printf(" Entering rmdir\n");
            removedir(ppathStack,netfd,&cmd,mysql);
        }
    

        if(cmd.cmdType == CMD_QUIT){
            //退出成功需要跳出循环释放线程资源
           int ret= quitCurrentUser(netfd,&cmd);   
           if(ret==0)
               return 1;
        }
       
        if(cmd.cmdType == CMD_DELUSER){
           int ret= deleteCurrentUser(netfd,&cmd,mysql);
           if(ret =0){
               //正常退出，已经关闭netfd
               return 2;
           }
        }
       
    }
}

